package com.two.screen.water.balance.net.observer


import android.app.Activity
import com.two.screen.water.balance.utils.CommUtils
import com.two.screen.water.balance.utils.ToastUtil
import com.two.screen.water.balance.widget.dialog.LoadingDialog
import io.reactivex.disposables.Disposable

/**
 * 创建者     Noel
 * 创建时间   2018/1/20 16:55
 * 描述	      对于上传需要弹出对话框
 */

abstract class UploadImgObserver<T>(activity: Activity) : BaseObserver<T>(activity) {

    private val loadingDialog: LoadingDialog?

    init {
        loadingDialog = LoadingDialog(activity)
    }

    override fun onSubscribe(d: Disposable) {
        mDisposable = d
        if (loadingDialog != null && !loadingDialog.isShowing) {
            loadingDialog.show()
        }
    }

    override fun onError(e: Throwable) {
        ToastUtil.showToast(e.message)
        if (!isHandleRequestMultiData()) {
            mDisposable!!.dispose()
        }
        onUploadComplete()
        if (loadingDialog != null && loadingDialog.isShowing) {
            loadingDialog.dismiss()
        }
        CommUtils.println("upload:onError->", e.toString())
    }

    override fun onComplete() {
        onUploadComplete()
        //网络请求结束后关闭对话框
        if (loadingDialog != null && loadingDialog.isShowing) {
            loadingDialog.dismiss()
        }
    }

    /*并发上传，若有一个失败，则立即中断，调用此方法，如果全成功后也会掉此方法*/
    abstract fun onUploadComplete()

    //override fun isHandleRequestMultiData(): Boolean = true

}
